# get GPS of cross-road of qingdao
# tang-xg@qq.com 20200404
# 依次将找到的路口gps点存下来，待续，高德路口更全，改用高德去

#https://developer.baidu.com/map/skins/MySkin/resources/iframs/webservice-placapi.html
#http://api.map.baidu.com/place/search?&query='福州路'&region='青岛'&output=xml&key=37492c0ee6f924cb5e934fa08c6b1676


import numpy as np
import pandas as pd 
import os
import pickle as pkl

os.chdir("/home/ubuntu/txg/traf_qingdao/semi-final")

#1 import meta-data
pwd = os.getcwd()
train_path = pwd + "/train/"
test_path = pwd + "/test_user/"
# load data of preliminary contest
[flow,relate_cross,crossName,target_cross,pred_cross,similar_cross,roadNet,submitList0,data_tr]=pkl.load(open(pwd+"/../data.pkl",'rb'))
gps_cross = set(target_cross) | set(pred_cross) # merge, gps_cross: cross need gps
gps_road = set() # gps_road: road need gps
crossName.index = crossName['crossroadID']
for gc in gps_cross:
    cross = crossName.loc[gc]
    gps_road.add(cross['road1'])
    gps_road.add(cross['road2'])


# =============================================================================
# from https://developer.baidu.com/map/place-api.htm
# http://api.map.baidu.com/place/search?&query=福州路&region=青岛&output=json&
# key=37492c0ee6f924cb5e934fa08c6b1676
# =============================================================================
""""用get方法请求api：这种方式把参数放在直接放在url中"""
import urllib.request,urllib,json# 把这两个库导入
url ='http://api.map.baidu.com/place/search'
for road in gps_road:
    data = {'query':road,'region':'青岛','output':'json','key':'37492c0ee6f924cb5e934fa08c6b1676'}
    data = urllib.parse.urlencode(data)
    url = url +'?'+ data
    #.... 依次将找到的路口gps点存下来，待续，高德路口更全，改用高德去
    
    
    
    
    
data = {'query':'福州路','region':'青岛','output':'json','key':'37492c0ee6f924cb5e934fa08c6b1676'}
data = urllib.parse.urlencode(data)
url = url +'?'+ data
""""跟post不同的只有这一句，使用?把url和data的内容连接起来，
结果是https://api.douban.com/v2/book/user/ahbei/collections?status=read&rating=3&tag=%E5%B0%8F%E8%AF%B4"""
response = urllib.request.urlopen(url)# 用.urlopen打开上一步返回的结果，得到请求后的响应内容
apicontent = response.read()#将响应内容用read()读取出来
print('用get方法请求api')
#print(apicontent)#打印读取到的内容 
ddict = json.loads(apicontent)
for k in range(len(ddict['results'])):
    ddict['results'][k]['address']




# =============================================================================
# """"用post方法请求api：这种方式把参数放在请求内容中传递，比较安全"""
# import urllib.request,urllib# 把这两个库导入
# 
# url ='https://api.douban.com/v2/book/user/ahbei/collections'# 这是要请求的url
# data={'status':'read','rating':4,'tag':'小说'}# 根据api文档提供的参数，我们来获取一下阿北读过的书中，他标记了‘小说’这个标签的三星书籍，把这些参数值存在一个dict里
# #data = urllib.parse.urlencode(data) # 把参数进行编码
# data = urllib.parse.urlencode(data).encode(encoding='UTF8')
# url2 = urllib.request.Request(url,data)# 用.Request来发送POST请求，指明请求目标是之前定义过的url，请求内容放在data里
# 
# response = urllib.request.urlopen(url2)# 用.urlopen打开上一步返回的结果，得到请求后的响应内容
# apicontent = response.read()#将响应内容用read()读取出来
# print('用post方法请求api')
# print(apicontent)#打印读取到的内容
# =============================================================================




